package com.example.calculator30;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;


public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button button3;
    private Button button4;
    private Button c;
    private Button ce;
    private Button plus;
    private Button b1;
    private Button b2;
    private Button b3;
    private Button minus;
    private Button b4;
    private Button b5;
    private Button b6;
    private Button multiply;
    private Button b7;
    private Button b8;
    private Button b9;
    private Button divison;
    private Button dot;
    private Button b0;
    private Button equal;
    private GridLayout GridLayout1;
    private String showText = "",result="";
    ScriptEngine scriptEngine=new ScriptEngineManager().getEngineByName("javascript");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_xml);
        GridLayout1 = (GridLayout) findViewById(R.id.GridLayout1);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        c = (Button) findViewById(R.id.c);
        ce = (Button) findViewById(R.id.ce);
        plus = (Button) findViewById(R.id.plus);
        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b3 = (Button) findViewById(R.id.b3);
        minus = (Button) findViewById(R.id.minus);
        b4 = (Button) findViewById(R.id.b4);
        b5 = (Button) findViewById(R.id.b5);
        b6 = (Button) findViewById(R.id.b6);
        multiply = (Button) findViewById(R.id.multiply);
        b7 = (Button) findViewById(R.id.b7);
        b8 = (Button) findViewById(R.id.b8);
        b9 = (Button) findViewById(R.id.b9);
        divison = (Button) findViewById(R.id.divison);
        dot = (Button) findViewById(R.id.dot);
        b0 = (Button) findViewById(R.id.b0);
        equal = (Button) findViewById(R.id.equal);
        textView = (TextView) findViewById(R.id.textView);
        button3.setOnClickListener(new textOnClickListener());
        b1.setOnClickListener(new textOnClickListener());
        b2.setOnClickListener(new textOnClickListener());
        b3.setOnClickListener(new textOnClickListener());
        b4.setOnClickListener(new textOnClickListener());
        b5.setOnClickListener(new textOnClickListener());
        b6.setOnClickListener(new textOnClickListener());
        b7.setOnClickListener(new textOnClickListener());
        b8.setOnClickListener(new textOnClickListener());
        b9.setOnClickListener(new textOnClickListener());
        b0.setOnClickListener(new textOnClickListener());
        button3.setOnClickListener(new textOnClickListener());
        plus.setOnClickListener(new textOnClickListener());
        minus.setOnClickListener(new textOnClickListener());
        divison.setOnClickListener(new textOnClickListener());
        multiply.setOnClickListener(new textOnClickListener());
        button4.setOnClickListener(new textOnClickListener());
        dot.setOnClickListener(new textOnClickListener());
        b0.setOnClickListener(new textOnClickListener());
        equal.setOnClickListener(v -> {
            String t=showText;
            try {
                result=scriptEngine.eval(showText).toString();
                showText=showText+"="+result;
                textView.setText(showText);
            } catch (ScriptException e) {
                e.printStackTrace();
            }
        });
        ce.setOnClickListener(v -> {
            showText = "";
            textView.setText(showText);
        });
        c.setOnClickListener(v -> {
            if(showText.isEmpty()){
                return;
            }
            showText = showText.substring(0, showText.length() - 1);
            textView.setText(showText);
        });

    }

    private class textOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String c = ((Button) v).getText().toString();
            showText = showText + c;
            textView.setText(showText);
        }
    }
}